package com.kinghunt.misp.service.impl.providex;

import com.bygoodtech.kings.base.utils.ConvertUtil;
import com.bygoodtech.kings.common.dto.ApiResult;
import com.bygoodtech.kings.common.dto.page.PageInfo;
import com.kinghunt.misp.common.dto.providex.ProvideDTO;
import com.kinghunt.misp.common.param.providex.ProvideParam;
import com.kinghunt.misp.dal.dao.providex.ProvideDAO;
import com.kinghunt.misp.dal.model.custx.CustDO;
import com.kinghunt.misp.dal.model.providex.ProvideDO;
import com.kinghunt.misp.service.api.providex.ProvideService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * 客户管理
 *
 * @Author ciaran
 * @Date 2023/7/13 21:58l
 */
@Slf4j
@Service
@Transactional
public class ProvideServiceImpl implements ProvideService {

    @Autowired(required = false)
    private ProvideDAO provideDAO;

    @Override
    public ApiResult<PageInfo<ProvideDTO>> getProvideList(ProvideParam provideParam) {
        int offset = (provideParam.getPageNum() - 1) * provideParam.getPageSize();
        ProvideDO provideDO = ConvertUtil.convert(provideParam, ProvideDO.class);
        int count = provideDAO.selectProvideListCount(provideDO);
        PageInfo<ProvideDTO> pageInfo = new PageInfo<>();
        pageInfo.setTotal(count);
        pageInfo.setPageNum(provideParam.getPageNum());
        pageInfo.setPageSize(provideParam.getPageSize());
        List<ProvideDO> provideDOList = provideDAO.selectProvideList(provideDO, offset, provideParam.getPageSize());
        List<ProvideDTO> dto =  ConvertUtil.convert(provideDOList, ProvideDTO.class);
        pageInfo.setData(dto);
        return ApiResult.ok(pageInfo);
    }

    @Override
    public ApiResult<ProvideDTO> getProvideDetail(Long provideId) {
        ProvideDO provideDO = provideDAO.selectByPrimaryKey(provideId);
        ProvideDTO provideDTO = ConvertUtil.convert(provideDO, ProvideDTO.class);
        return ApiResult.ok(provideDTO);
    }

    @Override
    public ApiResult addProvide(ProvideParam provideParam) {
        //判断供应商编号是否唯一
        List<ProvideDO> provideDOList = provideDAO.selectByProvideNo(provideParam.getProvideNo());
        if(provideDOList != null && provideDOList.size() > 0){
            return ApiResult.failOfMessage("您新增的供应商编号已存在！");
        }
        ProvideDO provideDO = ConvertUtil.convert(provideParam, ProvideDO.class);
        // 保存地址到数据库
        provideDAO.insert(provideDO);
        return ApiResult.ok();
    }

    @Override
    public ApiResult updateProvide(ProvideParam provideParam) {
        ProvideDO provideDO = provideDAO.selectByPrimaryKey(provideParam.getProvideId());
        if(null == provideDO){
            return ApiResult.failOfMessage("您修改的供应商不存在！ ");
        }
        // 修改当前地址
        ProvideDO updateProvideDO = ConvertUtil.convert(provideParam, ProvideDO.class);
        provideDAO.updateByPrimaryKeySelective(updateProvideDO);
        return ApiResult.ok();
    }

    @Override
    public ApiResult<Boolean> deleteProvide(Long ProvideId) {
        ProvideDO provideDO = provideDAO.selectByPrimaryKey(ProvideId);
        if(null == provideDO){
            return ApiResult.failOfMessage("您删除的供应商不存在！ ");
        }
        provideDAO.deleteByPrimaryKey(ProvideId);
        return ApiResult.ok();
    }

    @Override
    public ApiResult<List<ProvideDTO>> getProvideByIds(List<Long> provideIds) {
        if (CollectionUtils.isEmpty(provideIds)) {
            log.warn("getProvideByIds 入参为空");
            return ApiResult.failOfMessage("您查找的供应商参数不正确！ ");
        }
        List<ProvideDO> provideDOList = provideDAO.selectByIds(provideIds);
        List<ProvideDTO> dto =  ConvertUtil.convert(provideDOList, ProvideDTO.class);
        return ApiResult.ok(dto);
    }
}
